package com.wxh.multithread;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UnSafeList
 * @Description: 不安全的集合
 * @Author wxh
 * @Date: 2022/1/11 10:26
 * @Version V1.0.0
 * @Since 1.8
 */
public class UnSafeList {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>(10);
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }

        // 模拟延时
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 正确的值是 10000
        System.out.println(list.size());
    }
}
